package com.luck.easyfoodbatch.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.MyOrdersM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class OrderItemAdapter extends RecyclerView.Adapter<OrderItemAdapter.ViewItemHolder> {
    private Context mContext;
    private List<MyOrdersM.DataBean.ModelsBean.OrderInfoBean> list = new ArrayList<>();
    private int index;
       protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    @Override
    public ViewItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.my_order_item, null);
        return new ViewItemHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewItemHolder holder, int position) {

        final MyOrdersM.DataBean.ModelsBean.OrderInfoBean orderInfo = list.get(position);

        if (orderInfo != null && list.size() > 0) {
            MyOrdersM.DataBean.ModelsBean.OrderInfoBean infoBean = list.get(position);
            String goods_name = infoBean.getGoods_name();
            String propertyTags = infoBean.getPropertyTags();
            String goods_thumb = infoBean.getGoods_thumb();
            if (!Tools.isNull(goods_thumb)) {
                String path = "";
                if (goods_thumb.startsWith("http")) {
                    path = goods_thumb;
                } else {
                    path = HttpUtil.IMG_THUMB_BASE_URL + goods_thumb;
                }
                Glide.with(mContext)
                        .load(path)
                        .centerCrop()
                        .placeholder(R.mipmap.loading_4b3)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(holder.iv_banner);
            } else {
                holder.iv_banner.setImageResource(R.mipmap.loading_4b3);
            }

            int due_date = infoBean.getDue_date();
            if (!Tools.isNull(goods_name)) {
                holder.tv_title.setText(goods_name);
            } else {
                holder.tv_title.setText("");
            }
            holder.tv_time.setText("保质期 至 " + Tools.SubTime2(due_date + ""));
            holder.tv_spec.setText(propertyTags);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onItemClick(index, view);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public OrderItemAdapter(Context context, int index, List<MyOrdersM.DataBean.ModelsBean.OrderInfoBean> list) {
        this.mContext = context;
        this.list = list;
        this.index = index;
    }


    public class ViewItemHolder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_time, tv_spec;

        ImageView iv_banner;

        public ViewItemHolder(View view) {
            super(view);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            iv_banner = (ImageView) view.findViewById(R.id.iv_banner);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_spec = (TextView) view.findViewById(R.id.tv_spec);

        }
    }
}
